<?php
declare(strict_types=1);

namespace app\common\library\sms\engine;

use app\common\library\helper;
use app\common\library\sms\package\aliyun\SignatureHelper;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
use TencentCloud\Sms\V20210111\SmsClient;

/**
 * 腾讯云短信模块引擎
 * Class Tencent
 * @package app\common\library\sms\engine
 */
class Tencent extends Server
{
    private $config;

    /**
     * 构造方法
     * @param $config
     */
    public function __construct($config)
    {
        $this->config = $config;
    }

    private function getClient(): SmsClient
    {
        $cred = new Credential($this->config['SecretId'], $this->config['SecretKey']);
        $httpProfile = new HttpProfile();
        $httpProfile->setEndpoint("sms.tencentcloudapi.com");
        $clientProfile = new ClientProfile();
        $clientProfile->setSignMethod("TC3-HMAC-SHA256");
        $clientProfile->setHttpProfile($httpProfile);
        return new SmsClient($cred, $this->config['Region'], $clientProfile);
    }

    /**
     * 发送短信通知
     * @param array $sceneConfig 场景配置
     * @param array $templateParams 短信模板参数
     * @return bool
     */
    public function sendSms(array $sceneConfig, array $templateParams): bool
    {
        $client = $this->getClient();
        $req = new SendSmsRequest();
        $req->SmsSdkAppId = $this->config['AppId'];
        $req->SignName = $this->config['sign'];
        $req->PhoneNumberSet = ['+86' . $sceneConfig['acceptPhone']];
        $req->TemplateId = $sceneConfig['templateCode'];
        $params = [];
        foreach ($templateParams as $value) {
            $params[] = strval($value);
        }
        $req->TemplateParamSet = $params;
        $resp = $client->SendSms($req);
        return ($resp->getSendStatusSet()[0])->Code == 'Ok';
    }
}
